package io.publish.example;

import io.publish.mqtt.MqttClient;
import io.publish.mqtt.MqttConnectOptions;
import io.publish.mqtt.utils.Log;

public class App {

    public static void main(String[] args) throws Exception {
        //推送消息测试
        System.out.println("--------------------------------------------------------------------------------");
//        test1("127.0.0.1",8888,"publish",0,"topic1","你好！netty ———— mqtt");
//        test1("127.0.0.1",8888,"publish",0,"topic1","你好！netty ———— mqtt");
        test1("127.0.0.1",8888,"publish2",0,"topic2","你好！netty ———— mqtt");




    }

    /**
     *
     * @param host ip
     * @param port 端口号
     * @param clintId 客户端id标识
     * @param qos qos质量等级
     * @param topic 主题
     * @param msg 消息
     */
    public static void test1(String host,int port,String clintId,int qos,String topic,String msg){
        Log.enablePing(true); //是否打印ping日志（心跳检测）
        MqttClient mqttClient = new MqttClient();
        MqttConnectOptions options = new MqttConnectOptions();

        // emqx
        options.setHost(host);
        options.setPort(port);

        // apollo
//        options.setHost("localhost");
//        options.setPort(61613);
//        options.setUserName("admin");
//        options.setPassword("password".getBytes(StandardCharsets.UTF_8));

        options.setClientIdentifier(clintId);
        options.setKeepAliveTime(5);
        options.setCleanSession(true);
        // 配置动作超时时间
        mqttClient.setActionTimeout(3000);
        // 配置掉线重连
//        mqttClient.setReconnectOnLost(1, 10000);

        mqttClient.setCallback(new MqttClient.Callback() {
            @Override
            public void onConnected() {
                // test
                try {
                    // 订阅主题
                    mqttClient.subscribe(qos, topic);
                    // 订阅主题 topic 中可使用 /# ，表示模糊匹配该主题
                    // 示例：订阅主题 topic1/# ，可接收 topic1、
                    // topic1/aaa、topic1/bbb等主题下消息
//                    mqttClient.subscribe("topic");
                    // 发布一个消息到主题topic1/aaa
//                    mqttClient.publish("topic1/aaa", "hello, netty mqtt!-2-");
                    //发布消息
                    mqttClient.publish(topic, msg);


                    // 取消订阅
//                    mqttClient.unsubscribe("topic1");

//                    mqttClient.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onConnectFailed(Throwable e) {

            }

            @Override
            public void onConnectLost(Throwable e) {
                Log.i("-->断开连接 : " + e);
            }

            @Override
            public void onReconnectStart(int cur) {

            }

            @Override
            public void onMessageArrived(String topic, String s) {

            }
        });

        try {
            mqttClient.connect(options);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("--->连接失败了：" + e);
        }
    }

    public static void test2(){
        Log.enablePing(false);
        MqttClient mqttClient = new MqttClient();
        MqttConnectOptions options = new MqttConnectOptions();

        // emqx
        options.setHost("127.0.0.1");
        options.setPort(8888);

        // apollo
//        options.setHost("localhost");
//        options.setPort(61613);
//        options.setUserName("admin");
//        options.setPassword("password".getBytes(StandardCharsets.UTF_8));

        options.setClientIdentifier("netty_mqtt_c2");
        options.setKeepAliveTime(5);
        options.setCleanSession(true);
        // 配置动作超时时间
        mqttClient.setActionTimeout(3000);
        // 配置掉线重连
        mqttClient.setReconnectOnLost(1, 10000);

        mqttClient.setCallback(new MqttClient.Callback() {
            @Override
            public void onConnected() {
                // test
                try {
                    mqttClient.subscribe(1, "topic1");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onConnectFailed(Throwable e) {

            }

            @Override
            public void onConnectLost(Throwable e) {
                Log.i("-->断开连接 : " + e);
            }

            @Override
            public void onReconnectStart(int cur) {

            }

            @Override
            public void onMessageArrived(String topic, String s) {

            }
        });

        try {
            mqttClient.connect(options);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("--->连接失败了：" + e);
        }
    }
}
